<?
//SETUP MYSQL STUFF (for user storage, etc...)
define("G" , "src/");

define("DB_HOST" , "localhost");
define("DB_USER" , "root");
define("DB_PASS" , "sokhatai");
define("DB_NAME" , "amess");

require_once(G. "phpmailer/class.phpmailer.php");

function in($var){
	$link = mysql_connect(DB_HOST , DB_USER , DB_PASS);
	mysql_select_db(DB_NAME , $link); 
	$returned = strip_tags(mysql_real_escape_string($var)); return $returned;
	mysql_close();
}
function out($var){
	$link = mysql_connect(DB_HOST , DB_USER , DB_PASS);
	mysql_select_db(DB_NAME , $link);
	$returned = stripslashes(strip_tags(stripslashes($var))); return $returned;
	mysql_close(); 
}
function cmp($var){
	$link = mysql_connect(DB_HOST , DB_USER , DB_PASS);
	mysql_select_db(DB_NAME , $link);
	$returned = mysql_real_escape_string(strip_tags($var)); return $returned;
	mysql_close(); 
}


class Server{
	public $socket;
	public $data = array();
	public $secrets = array();
	public $i = 0;

	public function __construct($ip, $port){
		$this->socket = stream_socket_server("tcp://".$ip.":".$port, $errno, $errstr);
		if (!$this->socket) {
			die($errstr." - ".($errno)."\n");
		}else{
			$io = Gtk::io_add_watch($this->socket, Gobject::IO_IN, array($this,"socket_accept"));
		}
		
		//Initiate the Scheduled Command for sending out the database email queue.
		//$ten = 1000*60*10;
		$ten = 1000*60*10;
		Gtk::timeout_add($ten, array($this, 'onTimeout'));
		
		return true;
	}
	//The method which is a scheduled command
	public function onTimeout(){
		$link = mysql_connect(DB_HOST , DB_USER , DB_PASS);
		mysql_select_db(DB_NAME , $link);
		
		$selPrev = mysql_query("SELECT DISTINCT FORUSER FROM offlines WHERE sent='0'");
		while($resPrev = mysql_fetch_array($selPrev)){
			$subject  = "New instant messages from A-MESS";
			$message  = "Welcome to A-MESS\n\n==============\n";
			$message .= "Below are the messages, that were sent to you from your buddies, while you were offline\n\n";
			
			$sel = mysql_query("SELECT * FROM offlines WHERE sent = '0' AND FORUSER = '".cmp($resPrev['FORUSER'])."'");
			while($res = mysql_fetch_array($sel)){
				
				$seluser = mysql_query("SELECT email FROM users WHERE username = '".cmp($res['FROMUSER'])."'");
				$resuser = mysql_fetch_array($seluser);
				
				$message .= "(" . date("m/d/Y - H:i:s", $res['DATE']) . ") >> " . $resuser['email']. " >> " .$res['FROMUSER']."  >> ".out($res['MSG'])."\n";
				
				mysql_query("UPDATE offlines SET sent = '1' WHERE ID = '".cmp($res['ID'])."'");
			}
			
			$message .= "===========\n\nDownload A-MESS Messenger NOW from: http://www.a-mazed.com\n";
			$message .= "Best Regards,\nThe A-MESS Team";
			
			$selTo = mysql_query("SELECT email FROM users WHERE username = '".cmp($resPrev['FORUSER'])."'"); 
			$resTo = mysql_fetch_array($selTo);
			
			$mail = new PHPMailer();
			$mail->IsSMTP();
			$mail->Host = "smtp.gmail.com";
			$mail->SMTPSecure = "tls";
			$mail->Port=465;
			$mail->SMTPAuth = true;
			$mail->Username = "phpcip@gmail.com";
			$mail->Password = "sokhatai";
			$mail->From = "phpcip@gmail.com";
			$mail->FromName = "A-MESS Administrator";
			$mail->AddAddress(out($resTo['email']),out($resPrev['FORUSER']));
			$mail->Subject = $subject;
			$mail->Body = $message;
			$mail->AddReplyTo("phpcip@gmail.com","A-MESS Administrator");
			$mail->IsHTML(false);
			if(!$mail->Send()){
				//echo "Message was not sent\n";
				//echo "Mailer Error: " . $mail->ErrorInfo."\n";
			}
			unset($mail);
		}
		
		mysql_close();
		
		
		
		$link = mysql_connect(DB_HOST , DB_USER , DB_PASS);
		mysql_select_db(DB_NAME , $link);
		
		$selA = mysql_query("SELECT * FROM users WHERE sent = '0'");
		while($resA = mysql_fetch_array($selA)){
			$subjectA  = "Welcome to A-MESS";
			$messageA  = "A-MESS Account Information\n\n===========\n";
			$messageA .= "Username: ".$resA['username']."\n";
			$messageA .= "Password: the one you have chosen... Even WE don't know it...\n";
			$messageA .= "===========\n\nDownload A-MESS Messenger NOW from: http://www.a-mazed.com\n";
			$messageA .= "Best Regards,\nThe A-MESS Team";
			
			$mail = new PHPMailer();
			$mail->IsSMTP();
			$mail->Host = "smtp.gmail.com";
			$mail->SMTPSecure = "tls";
			$mail->Port=465; 
			$mail->SMTPAuth = true;
			$mail->Username = "phpcip@gmail.com";
			$mail->Password = "sokhatai";
			$mail->From = "phpcip@gmail.com";
			$mail->FromName = "A-MESS Administrator";
			$mail->AddAddress(out($resA['email']),out($resA['username']));
			$mail->Subject = $subjectA;
			$mail->Body = $messageA;
			$mail->AddReplyTo("phpcip@gmail.com","A-MESS Administrator");
			$mail->IsHTML(false);
			if(!$mail->Send()){
				//echo "Message was not sent\n";
				//echo "Mailer Error: " . $mail->ErrorInfo."\n";
			}
			unset($mail);
			mysql_query("UPDATE users SET sent='1' WHERE ID = '".$resA['ID']."'");
		}
		
		mysql_query("DELETE FROM offlines WHERE sent = '1'");
		
		mysql_close();
		
		return true;
	}

	
	
	
	
	
	public function socket_accept(){
		$dt = array();

		$newc = stream_socket_accept($this->socket);
		//This can use a bit of tweaking... To receive more or less of the info that comes from the client... 
		//Not so safe to receive a high amount of info in one go. might wanna have it truncated or something...
		
		$got = fread($newc , 1024);
		
		
		$gotArr = explode("|", $got);

		$dt['CLN'] = $newc;
		$dt['TYP'] = $gotArr[0];
		$dt['UID'] = $gotArr[1];
		$dt['MSG'] = $gotArr[2];

		$dt['MSG'] = str_replace("\n", "", $dt['MSG']);
		$msgArr = explode("#", $dt['MSG']);
		
		
		
		
		//0. We process the software update here...
		if($dt['TYP'] == "UPDATE"){
			$ini = file_get_contents("updates/updates.ini");
			$iniArr = explode("\n" , $ini);
			$server_version = $iniArr[0];
			
			$client_version = $dt['MSG'];
			
			if($server_version > $client_version){
				//Send the client software update...
				if($dt['UID'] == 'CLN'){
					if(is_file("updates/client.php")){
						$to_put = file_get_contents("updates/client.php");
						$send = $server_version . "<><||><>OK<><||><>" . $to_put;
					}else{
						unset($send);
						$send = $server_version . "<><||><>NO";
					}
					fwrite($dt['CLN'] , $send);
					usleep(80000);
					unset($send);
				}
				
				
				
				
				
				//Send the ressources update... Whatever's found in the images directory.
				if($dt['UID'] == 'RES'){
					$send = $server_version . "<><||><>OK<><||><>";
					//Read updates/images directory contents...
					$dir = "updates/images";
					$to_send = array();
					if($handle = opendir($dir)){
					    while($file = readdir($handle)){
					        clearstatcache();
					        if(is_file($dir.'/'.$file)){
					            $to_send[] = $file . "<<||FILENAME_DELIMITER||>>" . file_get_contents($dir.'/'.$file);
					        }
					    }
					    closedir($handle);
					}
					$files_to_send = implode("<<||<><>||||69||||<><>||>>", $to_send);
					$send .= $files_to_send;
					
					if(count($to_send) == 0){
						unset($send);
						$send = $server_version . "<><||><>NO";
					}
					
					fwrite($dt['CLN'] , $send);
					usleep(80000);
					unset($send);
					unset($files_to_send);
					unset($to_send);
				}
				
			}else{
				$send = $server_version . "<><||><>NO";
				fwrite($dt['CLN'] , $send);
				unset($send);
			}
			unset($dt);
		}
		
		
		
		
		


		//1. Process Authentications
		if($dt['TYP'] == "AUTH"){
			$userAuth = $msgArr[0];
			$passAuth = $msgArr[1];
			
			$link = mysql_connect(DB_HOST , DB_USER , DB_PASS);
			mysql_select_db(DB_NAME , $link);
		
			$sel = mysql_query("SELECT * FROM users WHERE username = '".cmp($userAuth)."' AND password = '".cmp($passAuth)."'");
			$res = mysql_fetch_array($sel);
			@mysql_free_result($sel);
			if($res['ID']){
				//We give back the OK signal plus the buddies list
				$strBack = $res['type']."|OK|";
				$selBud = mysql_query("SELECT BUID FROM buddies WHERE UID = '".$res['ID']."'");
				while($resBud = mysql_fetch_array($selBud)){
					$selUserName = mysql_query("SELECT username FROM users WHERE ID = '".$resBud['BUID']."'");
					$resUserName = mysql_fetch_array($selUserName);
					$strBack .= $resUserName['username']."#";
				}
				@mysql_free_result($selBud);
				$strBack .= "\n";
				fwrite($dt['CLN'], $strBack);
				$md5Secret = strtolower(md5($userAuth."^^".$passAuth));
				$this->secrets[] = $md5Secret;
			}else{
				fwrite($dt['CLN'], "5|ERR\n");
			}
			mysql_close();
		}

		
		
		
		
		
		
		//2. Process Greeting Message (LOGIN WELCOME)
		if($dt['TYP'] == "EHLO"){

			$secret = $dt['MSG'];
			$foundA = array_search($secret, $this->secrets);

			if($foundA !== false){
				unset($this->secrets[$foundA]);
				$this->secrets = array_values($this->secrets);
				
				

				//New generation of Would be nice to let his/her buddies know he's online...
				//Let me remove my zombie-like first.
				$ex_key = $this->array_search_recursive($dt['UID'] , $this->data);
				unset($this->data[$ex_key[0]]);
				$this->data = array_values($this->data);
				
				array_push($this->data, $dt); //we simply add the new user to the ONLINE users array.
				$this->user_status("ONLINE", $dt['UID']);
				
				//Now tell ME which of my list is online
				$cln_to_notify = $dt['CLN'];
				$my_buddies = $this->getMyBudds($dt['UID']);
				if(is_array($my_buddies)){
					if(count($my_buddies) > 0){
						foreach ($my_buddies as $my_not_bud){
							$found = $this->array_search_recursive($my_not_bud, $this->data);
							if($cln_to_notify && $this->data[$found[0]]['UID'] == $my_not_bud){
	
								$status_note = "0#IQ_STATS#".$my_not_bud."#ONLINE#0\n";
								
								$this->write($cln_to_notify, $dt['UID'] , $status_note);
								usleep(80000);
							}
						}
					}
				}
			}
		}
		///////////////////////////////////////////////////////
		
		
		
		
		
		
		
		
		

		//4. Process Messages
		if($dt['TYP'] == "IM"){
			$for_user = $msgArr[3];
			//echo $for_user;
			//Find out if the recipient is online
			$key = $this->array_search_recursive($for_user, $this->data);
			if($key[1] == "UID"){
				$cln_to_send_to = $this->data[$key[0]]['CLN'];
				$msg_to_send = $dt['MSG'];
				$this->write($cln_to_send_to, $for_user, $msg_to_send);
			}else{
				//Record the message in the database. It will be dispatched to the user via email once every 10 minutes.
				//Check last one
				$ago = time() - 1;
				
				$link = mysql_connect(DB_HOST , DB_USER , DB_PASS);
				mysql_select_db(DB_NAME , $link);
		
				$selLast = mysql_query("SELECT ID FROM offlines WHERE DATE >= '".$ago."'");
				$resLast = mysql_fetch_array($selLast);
				if(!$resLast['ID']){
					mysql_query("INSERT INTO offlines (FROMUSER , FORUSER , MSG , DATE) VALUES ('".in($dt['UID'])."','".in($for_user)."','".in($msgArr[5])."','".time()."')");
				}
				
				mysql_close();
			}
		}
		

		//5. Process any other sort of requests..
		//5.1 Process removal of buddies from the list.
		
		if($dt['TYP'] == "BUDREM"){
			//print_r($this->data);
			//print_r($dt);
			
			$link = mysql_connect(DB_HOST , DB_USER , DB_PASS);
			mysql_select_db(DB_NAME , $link); 
	
			$key = $this->array_search_recursive($dt['UID'] , $this->data); //Search to see if this user is logged into the system.
			//We do not allow for anonymous requests to server...
			$msg_arr = explode("#" , $dt['MSG']);
			if ($key !== false){ //If he is logged in
				$cln_to_notify = $this->data[$key[0]]['CLN'];
				$stored_md5 = $this->data[$key[0]]['MSG'];
				if($stored_md5 == $msg_arr[0]){
					$to_remove = explode(">>" , $msg_arr[1]);
					//Frist select the user ID
					$selUID = mysql_query("SELECT ID FROM users WHERE username = '".cmp($dt['UID'])."'");
					$resUID = mysql_fetch_array($selUID);
					$UID = $resUID['ID'];
					//Now parse all removal array and remove if they are really owned by the above ID
					foreach($to_remove as $removal){
						//Now select the buddy removal ID
						$selBudId = mysql_query("SELECT ID from users WHERE username = '".cmp($removal)."'");
						$resBudId = mysql_fetch_array($selBudId);
						$BUDID = $resBudId['ID'];
						$selEx = mysql_query("SELECT ID FROM buddies WHERE UID = '".$UID."' AND BUID = '".cmp($BUDID)."'");
						$resEx = mysql_fetch_array($selEx);
						if($resEx['ID']){
							mysql_query("DELETE FROM buddies WHERE ID = '".cmp($resEx['ID'])."'");
						}
					}
				}
			}
			mysql_close();
		}
			
		//5.2 Add a buddy to the list
		if($dt['TYP'] == "BUDADD"){
			$link = mysql_connect(DB_HOST , DB_USER , DB_PASS);
			mysql_select_db(DB_NAME , $link); 
	
			$key = $this->array_search_recursive($dt['UID'] , $this->data); //Search to see if this user is logged into the system.
			//We do not allow for anonymous requests to server...
			$msg_arr = explode("#" , $dt['MSG']);
			if ($key !== false){ //If he is logged in
				$cln_to_notify = $this->data[$key[0]]['CLN'];
				$stored_md5 = $this->data[$key[0]]['MSG'];
				if($msg_arr[1] != $dt['UID']){
					if($stored_md5 == $msg_arr[0]){
						//echo 'user-ul a trimis parola corecta si e logat.';
						//Check if user is already in the list.
						//First grab the added buddy's ID. If it doesn't exist we send error right away. If it does, we add it and send succes.
						$selBUID = mysql_query("SELECT ID FROM users WHERE username = '".cmp($msg_arr[1])."'");
						$resBUID = mysql_fetch_array($selBUID);
						if($resBUID['ID']){
							//echo 'buddy-ul exista in db';
							//First select the ID of the Requestor
							$selUID = mysql_query("SELECT ID FROM users WHERE username='".cmp($dt['UID'])."'");
							$resUID = mysql_fetch_array($selUID);
							$requestor_UID = $resUID['ID'];
							//If requestor exists
							if($requestor_UID){
								//echo 'requestor-ul exista in db';
								//Now check if that buddy doesn't already exist in the requestors list.
								$selEx = mysql_query("SELECT ID FROM buddies WHERE UID = '".cmp($requestor_UID)."' AND BUID = '".cmp($resBUID['ID'])."'");
								$resEx = mysql_fetch_array($selEx);
								if(!$resEx['ID']){ //If he doesn't already exist in the list...
									//echo 'buddy-ul nu e deja adaugat';
									//Send the OK to the client. It's OK to add to that list of his.
									if(fwrite($cln_to_notify, "0#BUDADD#SUCCESS#".$stored_md5."#\n") === false){
										unset($this->data[$key[0]]);
										$this->data = array_values($this->data);
									}
									mysql_query("INSERT INTO buddies (UID, BUID) VALUES ('".in($requestor_UID)."','".in($resBUID['ID'])."')"); //This is where we REALLY add the buddy to the DB.
									//echo 'buddy-ul a fost adaugat cu succes';
									
									//Now send the note to the buddy
									//First see if the user is online.
									$key_bud = $this->array_search_recursive($msg_arr[1] , $this->data);
									//print_r($key_bud);
									if ($key_bud !== false){ //If buddy is logged in that means he may receive this note now.
										$cln_bud_to_notify = $this->data[$key_bud['0']]['CLN'];
										//echo $cln_bud_to_notify;
										$this->write($cln_bud_to_notify, $msg_arr[1] , "0#YOUWEREADDED#".out($dt['UID'])."#\n");
										usleep(80000);
										//Let the requestor know that this recently added buddy is online.
										$this->write($cln_to_notify, $dt['UID'] , "0#IQ_STATS#".$msg_arr[1]."#ONLINE#0\n");
										usleep(80000);
									}else{
										//This means we have to store an offline note for the buddy. That will be delivered later via Process Messages...
										mysql_query("INSERT INTO offlines (FROMUSER , FORUSER , MSG , DATE) VALUES 
										('".in($dt['UID'])."','".in($msg_arr[1])."','I just added you to my buddies list dude ! Please do the same !!! NOW !!!','".time()."')");
									}
								}else{
									//User is there now
									$this->write($cln_to_notify, $dt['UID'] , "0#BUDADD#ERROR CODE 69:#".$stored_md5."#THAT USER IS ALREADY A BUDDY#\n");
									usleep(80000);
								}
							}
						}else{
							//User is NOT
							$this->write($cln_to_notify, $dt['UID'] , "0#BUDADD#ERROR CODE 69:#".$stored_md5."#THAT USER WAS NOT FOUND#\n");
							usleep(80000);
						}
					}
				}else{
					//Can not add self
					$this->write($cln_to_notify, $dt['UID'] , "0#BUDADD#ERROR CODE 6969:#".$stored_md5."#YOU MAY NOT ADD YOURSELF AS A BUDDY YOU SICK TWISTED NARCISIST !#\n");
					usleep(80000);
				}
			}
			mysql_close();
		}
		
		//5.3 Signup new user
		if($dt['TYP'] == "SIGNUP"){
			$link = mysql_connect(DB_HOST , DB_USER , DB_PASS);
			mysql_select_db(DB_NAME , $link); 
	
			$username = $msgArr[1];
			$password = $msgArr[2];
			$email = $msgArr[3];
			
			$username = substr($username , 0 , 12);
			$username = preg_replace('/[^a-z0-9]/', '', $username);
			
			$password = preg_replace('/[^a-z0-9]/', '', $password); 
			
			$email = substr($email , 0 , 120);
			
			$selEx = mysql_query("SELECT ID FROM users WHERE username = '".$username."'");
			$resEx = mysql_fetch_array($selEx);
			
			if(!$username){
				$this->write($dt['CLN'], $dt['UID'] , "ERROR#ERROR CODE 69: PLEASE FILL IN THE USERNAME#\n");
				usleep(80000);
			}
			if(!$password){
				$this->write($dt['CLN'], $dt['UID'] , "ERROR#ERROR CODE 69: PLEASE FILL IN THE PASSWORD#\n");
				usleep(80000);
			}
			if(!$email){
				$this->write($dt['CLN'], $dt['UID'] , "ERROR#ERROR CODE 69: PLEASE FILL IN THE EMAIL#\n");
				usleep(80000);
			}
			if($username && $password && $email){
				if(!$resEx['ID']){
					mysql_query("INSERT INTO users (username,password,email,sent) VALUES ('".in($username)."','".in($password)."','".in($email)."','0')");
					$this->write($dt['CLN'], $dt['UID'] , "SUCCESS#\n");
					usleep(80000);
				}else{
					//IS
					$this->write($dt['CLN'], $dt['UID'] , "ERROR#ERROR CODE 69: SELECTED USERNAME ALREADY EXISTS ! PLEASE SELECT ANOTHER USERNAME, TRY AGAIN, WHATEVER...#\n");
					usleep(80000);
				}
			}
			
			mysql_close();
			
			//print_r($dt);
		}
		

		

		//6. Process Client QUIT signal
		if($dt['TYP'] == "QUIT"){
			//First check for password. Don't allow users to signoff other users.
			//User has become zombie. We must kill him before he takes over the planet.
			$dead_user_key = $this->array_search_recursive($dt['UID'], $this->data);
			$storedMD5 = $this->data[$dead_user_key[0]]['MSG']; $hisMD5 = $dt['MSG'];
			
			if($hisMD5 == $storedMD5){
				$this->user_status("OFFLINE", $this->data[$dead_user_key[0]]['UID']);
				unset($this->data[$dead_user_key[0]]); //Remove zombie
				$this->data = array_values($this->data); //ReKey the main array $this->data
			}
		}
		//////////////////////////
				
		
		//print_r($this->data);
		unset($dt);
		return true;
		
		
		
	}
	
	
	
	public function user_status($status, $username){
	
		$link = mysql_connect(DB_HOST , DB_USER , DB_PASS);
		mysql_select_db(DB_NAME , $link); 
	
		//This method treats the status of a user and performs a set of vital actions.
		
		//First of all get my Client from $this->data
		$my_cln_key = $this->array_search_recursive($username , $this->data);
		$my_cln = $this->data[$my_cln_key[0]]['CLN'];
		
		//First get the buddies list
		$selUID = mysql_query("SELECT ID FROM users WHERE username = '".cmp($username)."'");
		$resUID = mysql_fetch_array($selUID);
		$UID = $resUID['ID'];
		
		//Notify those that added me that I am now online or offline.
		$buddies = array();
		$selBUDDIES = mysql_query("SELECT UID FROM buddies WHERE BUID = '".$UID."'");
		while($resBUDDIES = mysql_fetch_array($selBUDDIES)){
			$buddyID = out($resBUDDIES['UID']);
			//Select the username of the buddy
			$selUBuddy = mysql_query("SELECT username FROM users WHERE ID = '".$buddyID."'");
			$resUBuddy = mysql_fetch_array($selUBuddy);
			
			//See if buddy is online so we may notify. If he is offline it makes no sense in doing so. Create an array with the buddies that are online.
			$found = $this->array_search_recursive($resUBuddy['username'], $this->data);
			
			$cln_to_notify = $this->data[$found[0]]['CLN'];
			
			if($cln_to_notify && $this->data[$found[0]]['UID'] == $resUBuddy['username']){
				if($status == 'ONLINE'){
					$status_note = "0#IQ_STATS#".$username."#ONLINE#0\n";
				}
				if($status == 'OFFLINE'){
					$status_note = "0#IQ_STATS#".$username."#OFFLINE#0\n";
				}
				
				$this->write($cln_to_notify, $this->data[$found[0]]['UID'] , $status_note);
				usleep(80000);
			}
		}
		
		mysql_close();
	}
	
	
	
	public function getMyBudds($me){
		$link = mysql_connect(DB_HOST , DB_USER , DB_PASS);
		mysql_select_db(DB_NAME , $link); 
		
		$buddies = array();
		//1. get the ID of the parentUsername
		$selID = mysql_query("SELECT ID FROM users WHERE username = '".cmp($me)."'");
		$resID = mysql_fetch_array($selID);
		$UID = $resID['ID'];
		@mysql_free_result($selID);
	
		//2. get parentUsername's buddies IDs
		$selBid = mysql_query("SELECT BUID FROM buddies WHERE UID = '".$UID."'");
		while($resBid = mysql_fetch_array($selBid)){
			//3. get those buddies's usernames and pack them for an array that'll be ready to be returned
			$selUserName = mysql_query("SELECT username FROM users WHERE ID = '".$resBid['BUID']."'");
			$resUserName = mysql_fetch_array($selUserName);
			array_push($buddies, out($resUserName['username']));
		}
		@mysql_free_result($selBid);
		
		mysql_close();
		
		return $buddies;
	}
	
	
	public function write($to_socket, $to_username , $msg){
		$size = @fwrite($to_socket , $msg);
		if($size === false || $size == 0){
			//echo "nu pooot !";
			//See if the user we send the notification to is zombie: Hanged as online while his processed was somehow killed.
			$dead_user_key = $this->array_search_recursive($to_username, $this->data);
			
			//echo $this->data[$dead_user_key[0]]['UID'];
			//Now inform all the buddies of this user that HE is OFFline
			$this->user_status("OFFLINE", $this->data[$dead_user_key[0]]['UID']);
			
			unset($this->data[$dead_user_key[0]]); //Remove zombie
			$this->data = array_values($this->data); //ReKey the main array $this->data
			
			return false;
		}
		return true;
	}
	
	public function array_search_recursive($needle, $haystack, $path=array()){
    	foreach($haystack as $id => $val){
         $path2=$path;
         $path2[] = $id;
 
         if($val == $needle){
              return $path2;
         }else if(is_array($val)){
              if($ret = $this->array_search_recursive($needle, $val, $path2)){
                   return $ret;
              }
         }
      }
      return false;
	}
}

$S = new Server("78.97.196.201", 8000);
Gtk::main();
?>